/**
 * <p>
 * Title: 动态sql语句构造辅助类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2011
 * </p>
 * 
 * @author 赵明才
 * @version 1.0
 */
package atom.core0.jdbc;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import atom.core0.util.DateUtil;
import atom.core0.util.StringUtil;





//LinkedHashMap 是HashMap的一个子类，保存了记录的插入顺序，在用Iterator遍历LinkedHashMap时，先得到的记录肯定是先插入的
public class SqlUtil
{

	private static Logger logger = Logger.getLogger(SqlUtil.class);

	public static String getSelectSql(String userName,String tableName, LinkedHashMap whereMap, List<String> fieldList) 
	{
		String tableUser="";
		if (!StringUtil.isEmpty(userName))
			tableUser=userName+".";

		StringBuffer wherehql = new StringBuffer();
		StringBuffer fieldhql = new StringBuffer();

		boolean bFirst = true;
		for(int i=0;i<fieldList.size();i++)
		{
			
			if (bFirst)
			{
				bFirst = false;
			}
			else
			{
				fieldhql.append(" , ");
			}
			fieldhql.append(fieldList.get(i));
		}

		wherehql.append(" where  1=1 ");
		Iterator it = whereMap.entrySet().iterator();
		while (it.hasNext())
		{

			Map.Entry entry = (Map.Entry) it.next();
			String key = (String) entry.getKey();
			Object value = entry.getValue();

			wherehql.append(" and "+ key + "=" +getObjectValue(value));

		}

		

		String sql = "select " + fieldhql + " from "+ tableUser+tableName + wherehql;

		return sql;
	}
	
	public static String getUpdateSql(String userName,String tableName, LinkedHashMap whereMap, LinkedHashMap fieldvalueMap) 
	{
		String tableUser="";
		if (!StringUtil.isEmpty(userName))
			tableUser=userName+".";
		String sql = "update " + tableUser+tableName;

		StringBuffer wherehql = new StringBuffer();
		StringBuffer valuehql = new StringBuffer();
		wherehql.append(" where 1=1 ");
		valuehql.append(" set ");
		Iterator it = whereMap.entrySet().iterator();
		boolean bFirst = true;
		while (it.hasNext())
		{

			Map.Entry entry = (Map.Entry) it.next();
			String key = (String) entry.getKey();
			Object value = entry.getValue();

			wherehql.append(" and "+ key + "=" +getObjectValue(value));
			

		}

		it = fieldvalueMap.entrySet().iterator();
		bFirst = true;
		while (it.hasNext())
		{

			Map.Entry entry = (Map.Entry) it.next();
			String key = (String) entry.getKey();
			Object value = entry.getValue();

			if (bFirst)
			{
				bFirst = false;
			}
			else
			{
				valuehql.append(" , ");
			}
			valuehql.append(key + "=");
			valuehql.append(getObjectValue(value));
		}

		sql = sql + valuehql + wherehql;

		return sql;
	}

	// map 支持 String Double Integer Date
	public static String getInsertSql(String userName,String tableName, LinkedHashMap fieldvalueMap) 
	{
		String tableUser="";
		if (!StringUtil.isEmpty(userName))
			tableUser=userName+".";
	
		String hql = "insert into " + tableUser+tableName;
		StringBuffer fieldhql = new StringBuffer();
		StringBuffer valuehql = new StringBuffer();
		fieldhql.append("(");
		valuehql.append(" values (");
		Iterator it = fieldvalueMap.entrySet().iterator();
		boolean bFirst = true;
		while (it.hasNext())
		{

			Map.Entry entry = (Map.Entry) it.next();
			String key = (String) entry.getKey();
			Object value = entry.getValue();

			if (bFirst)
			{
				bFirst = false;
			}
			else
			{
				fieldhql.append(",");
				valuehql.append(",");
			}
			fieldhql.append(key);

			valuehql.append(getObjectValue(value));

		}

		fieldhql.append(")");
		valuehql.append(")");
		hql = hql + fieldhql + valuehql;
		return hql;
	}

	public static String getObjectValue(Object value)
	{
		String valuehql = "";
		if (value==null)
			return "null";
		
		if (value.equals("?"))
			return "?";
		
		if (value instanceof String)
		{
			String s = (String)value;
			if (s.length()>=1)
			{
				if (s.substring(0, 1).equals(":"))
					valuehql = s;
				else
					valuehql = "'" + value + "'";
			}
			else
			{
				valuehql = "'" + value + "'";
			}
		}
		else if (value instanceof Double)
			valuehql = String.format("%.4f",value);
		else if (value instanceof Integer)
			valuehql = String.valueOf(value);
		else if (value instanceof Long)
			valuehql = String.valueOf(value);
		else if (value instanceof Date)
			valuehql = "to_date('" + DateUtil.getDateTimeStr((Date) value,false) + "','YYYYMMDDHH24MISS')";
		else
			logger.error("SqlUtil不支持此类型的值的转换["+value.getClass()+"]"+value);
		return valuehql;
	}

}
